﻿using LightSO.Entity;
using LightSO.Entity.Enums;
using LightSO.ILibrary;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace LightSO.Extensions
{
    public static class RepositoryExtension
    {
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <typeparam name="T">EntityBase</typeparam>
        /// <param name="repo">数据仓储</param>
        /// <param name="desc">是否降序排序</param>
        /// <param name="sortField">排序字段表达式</param>
        /// <returns></returns>
        public static List<T> All<T>(this IRepository<T> repo, bool desc = false, Expression<Func<T, object>> sortField = null) where T : LightSO.ILibrary.EntityBase, new()
        {
            if (sortField != null)
            {
                if (desc)
                    return repo.AsQueryable().OrderByDescending(sortField).ToList();
                else
                    return repo.AsQueryable().OrderBy(sortField).ToList();
            }

            return repo.AsQueryable().Where(o=>1==1).ToList();
        }

        /// <summary>
        /// 公共更新数据方法
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <param name="mode">保存方式（Insert|Update）</param>
        /// <param name="exp">条件表达式</param>
        public static void Save<T>(this T entity, SaveMode mode, Expression<Func<T, bool>> exp = null) where T : LightSO.Entity.EntityBase, new()
        {
            var repo = SpringExtension.Ioc<IRepository<T>>();

            if (mode == SaveMode.Insert)
            {
                repo.Insert(entity as T);
                return;
            }

            if (exp != null)
                repo.Update(entity as T, exp);
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <typeparam name="T">EntityBase</typeparam>
        /// <param name="repo">数据仓储</param>
        /// <param name="desc">是否降序排序</param>
        /// <param name="sortField">排序字段表达式</param>
        /// <returns></returns>
        public static List<T> AllMongo<T>(this IMongoRepository<T> repo, bool desc = false, Expression<Func<T, object>> sortField = null) where T : LightSO.ILibrary.EntityBase, new()
        {
            if (sortField != null)
            {
                if (desc)
                    return repo.AsQueryable().OrderByDescending(sortField).ToList();
                else
                    return repo.AsQueryable().OrderBy(sortField).ToList();
            }

            return repo.AsQueryable().ToList();
        }

        /// <summary>
        /// 公共更新数据方法
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <param name="mode">保存方式（Insert|Update）</param>
        /// <param name="exp">条件表达式</param>
        public static void SaveMongo<T>(this T entity, SaveMode mode, Expression<Func<T, bool>> exp = null) where T : LightSO.Entity.EntityBase, new()
        {
            var repo = SpringExtension.Ioc<IMongoRepository<T>>();

            if (mode == SaveMode.Insert)
            {
                repo.Insert(entity as T);
                return;
            }

            if (exp != null)
                repo.Update(entity as T, exp);
        }
    }
}
